# @File :trainTicketPage.py
import time

from selenium.webdriver.common.by import By

from Pages import BasePage


class trainTicketPage(BasePage.Action):

    def __init__(self):
        #首页点击火车票元素
        self.trainIcon_xpath = (By.XPATH,"//li/span[text() = '火车票']")
        #出发日期元素
        self.GoDate_xpath = (By.XPATH,"//input[@placeholder = '时间/日期']")

        #出发日期 = 当前日期+12天
        self.dateTrainGoDay = time.strftime('%Y-%m-%d', time.localtime(time.time()+1036800))
        self.dateTrainGoDay_xpath = (By.XPATH,"//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']" % self.dateTrainGoDay)
        #改签出发日期=当前日期+10天
        self.dateTrainChangeDay = time.strftime('%Y-%m-%d', time.localtime(time.time()+864000))
        self.dateTrainChangeDay_xpath = (By.XPATH,"//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']" % self.dateTrainChangeDay)

        #出发城市、到达城市
        self.aimCity_xpath = (By.XPATH,"//input[@placeholder = '请选择目的地']")
        #到达城市
        #self.toCity_xpath = (By.XPATH,"//input[@placeholder = '请选择目的地']")
        #MNPQRS
        self.MNPQRSCity_xpath = (By.XPATH,"//div[@class='train-search']//li[contains(text(),'MNPQRS')]")
        #平凉
        self.pingLiang_xpath = (By.XPATH,"//div[@class='train-search']//span[@title = '平凉']")
        #平凉南
        self.pingLiangNan_xpath = (By.XPATH,"//div[@class='train-search']//span[@title = '平凉南']")

        #列表页预定元素
        self.haveTicketAndPreBuy_xpath = (By.XPATH,"//span[text() = '有票']/../../p[5]/span")
        #出票方式预定元素
        self.youxuan_xpath = (By.XPATH,"//div[text() = '预订']")
        #提交车票订单按钮元素
        self.submitTrainTicket_xpath = (By.XPATH,"//div[text() = '下一步']")
        #
        self.searchTrain_xpath = (By.XPATH, "//button[@class='search-btn']")     #//div[@class = 'train-search']/div/button[text() = '搜索']
        #改期占座成功
        self.changeSeatSuccess_xpath = (By.XPATH, "//div[@class='status change-seat']//label[@class='seat-icon seat-success']")
        #改期原因下拉选择框
        self.changeReason_xpath = (By.XPATH, "//input[@placeholder='请选择改签原因']")
        #改期原因选项
        self.changeReasonA_xpath = (By.XPATH, "//li/span")
        #确认改签
        self.confirmChange_xpath = (By.XPATH, "//span[@class='btn two']")
        #
        self.confirmChange2_xpath = (By.XPATH, "//button/span[text()='确认改签']")

    #提交火车票
    def click_submitTrainTicket(self):
        time.sleep(3)
        self.smart_click(*self.submitTrainTicket_xpath)
        time.sleep(3)
    #出票方式预定
    def click_youxuan(self):
        self.js_click(self.find_elements(*self.youxuan_xpath)[-1])
        time.sleep(2)
   #列表预定方法
    def selectTicket(self,index):
        self.js_click(self.find_elements(*self.haveTicketAndPreBuy_xpath)[index])
        time.sleep(3)
    #选择出发日期
    def selectGoDateForTrain(self, i):
        self.find_elements(*self.GoDate_xpath)[i].click()


    def select_TrainGoDate(self):
        self.find_elements(*self.dateTrainGoDay_xpath)[-2].click()
        time.sleep(2)

    def select_TrainFromCity(self):
        self.find_elements(*self.aimCity_xpath)[-3].click()
        self.find_elements(*self.MNPQRSCity_xpath)[0].click()
        time.sleep(2)
        self.find_elements(*self.pingLiang_xpath)[0].click()

    def select_TrainToCity(self):
        self.find_elements(*self.aimCity_xpath)[-2].click()
        self.find_elements(*self.MNPQRSCity_xpath)[1].click()
        time.sleep(2)
        self.find_elements(*self.pingLiangNan_xpath)[1].click()

    def click_TrainTicketIcon(self):
        self.smart_click(*self.trainIcon_xpath)

    def select_TrainChangeDate(self):
        self.selectGoDateForTrain(0)  #首页传-2，改期传0
        self.find_element(*self.dateTrainChangeDay_xpath).click()
        time.sleep(2)
        print("选择改签日期：")
        time.sleep(2)

    def click_SearchTrainButton(self):
        self.find_element(*self.searchTrain_xpath).click()
        time.sleep(10)

    def waitToConfirmChange(self):
        if self.find_element(*self.changeSeatSuccess_xpath):
            if self.find_element(*self.changeReason_xpath):
                self.find_element(*self.changeReason_xpath).click()
                time.sleep(1)
                self.find_elements(*self.changeReasonA_xpath)[0].click()
                time.sleep(1)
            self.find_element(*self.confirmChange_xpath).click()
            time.sleep(1)
            self.find_element(*self.confirmChange2_xpath).click()
            time.sleep(10)
        else:
            time.sleep(15)
            return self.waitToConfirmChange()
